//var staticLinks = ("cart");

var images = {};
var thumbs = {};
function useAjax(url, data, onsuccess) {
    $('#cart_loading').attr("src", "/Images/loading.gif");
    $('#cart_loading').css("display", "block");
    $.ajax({
        type: "POST",
        url: url,
        data: data,
        success: function (result) {
            $('#cart_loading').attr("src", "");
            $('#cart_loading').css("display", "none");
            if (onsuccess != null) onsuccess(result);
        },
        error: function (result) {
        }
    });
}

function changeItemsPerPage(el) {
    $.cookie("itemsPerPage", el.value);
    $(".changeItemsPerPage").val(el.value);
    var url = window.location.toString().split("#")[1];
    //alert(url);
    navigateTo(url?url:null);
    //el.value
}

function preloadImage(id, thumb, name) {
    if ((thumbs[name] != null) && (images[name] != null)) {
        $("#" + name).remove();
        $("#cont_" + id).append(thumbs[name]);
    } else {
        var initfunc = thumb ? initsmall : initlarge;
        var clickfunc = thumb ? "smallclicked" : "largeclicked";
        var collection = thumb ? thumbs : images;
        var onsuccess = function (result) {
            var image = $('<img id='+name+' onclick="'+clickfunc+'(this);" />').attr('src', result);
            initfunc(image, name);
            collection[name] = image;
            if (thumb)
                $("#" + name).replaceWith(image);
        }
        var url = "/Products/GetImage";
        var data = { "id": id, "thumb": thumb };
        useAjax(url, data, onsuccess);
    }
}

function addToCart(id) {
    var quantity = $("#Quantity_" + id).val();
    var dataToBeSent = { 'quantity': quantity, 'productId': id };
    var url = '/Cart/AddToCart/';
    useAjax(url, dataToBeSent,refreshCartHolder);
}

function initlarge(el) {
    el.css("position", "absolute");
    el.css("top", "15px");
    el.css("left", "15px");
    el.css("height", "90%");
    el.css("z-index", "2");
    el.css("cursor", "pointer");
}

function initsmall(el) {
    el.css("position", "relative");
    el.css("top", "0px");
    el.css("left", "0px");
    el.css("height", "75px");
    el.css("z-index", "1");
    el.css("cursor", "pointer");
}

function smallclicked(el) {
    var me = $("#" + el.id);
    var newel = images[el.id];
    me.replaceWith(newel);
}

function largeclicked(el) {
    var me = $("#" + el.id);
    var newel = thumbs[el.id];
    me.replaceWith(newel);
}

function navigateTo(url, returl) {
    //$('#main').hide();
    $(".selected").attr("class", "");
    var dataToBeSent = { "returnUrl": returl==null?null:returl.toString() };
    var onsuccess = function (result) {
        $('#main').hide().html(result).fadeIn();
    };
    useAjax(url, dataToBeSent, onsuccess);
    if(url.split("/").length<=1)
        $("#" + url).attr("class", "selected");
    else
        $("#" + url.split("/")[0]).attr("class", "selected");
}

function refreshCartHolder() {
    var url = '/Cart/Summary/';
    var onsuccess = function (result) {
        $('#cartholder').hide().html(result).fadeIn();
    }
    useAjax(url, null, onsuccess);
}

function removeFromCart(id, returnUrl) {
    var dataToBeSent = { 'productId': id, 'returnUrl': returnUrl };
    var url = '/Cart/RemoveFromCart/';
    var onsuccess = function (result) {
        $('#main').hide().html(result).fadeIn();
        refreshCartHolder();
    };
    useAjax(url, dataToBeSent, onsuccess);
}